package com.nsl.my_youlai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nsl.my_youlai.convert.MajorCategoryConverter;
import com.nsl.my_youlai.mapper.MajorCategoryMapper;
import com.nsl.my_youlai.model.entity.MajorCategoryEntity;
import com.nsl.my_youlai.model.vo.options.MajorCategoryElSelectOption;
import com.nsl.my_youlai.service.MajorCategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class MajorCategoryServiceImpl extends ServiceImpl<MajorCategoryMapper, MajorCategoryEntity> implements MajorCategoryService {



    @Override
    public List<MajorCategoryElSelectOption> listByLevel(Integer level) {
        LambdaQueryWrapper<MajorCategoryEntity> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(MajorCategoryEntity::getLevel,level).orderByAsc(MajorCategoryEntity::getLevel).orderByAsc(MajorCategoryEntity::getLevelSort);

        List<MajorCategoryEntity> majorCategoryEntityList=baseMapper.selectList(wrapper);


        log.info("majorCategoryEntityList = {}",majorCategoryEntityList);
        List<MajorCategoryElSelectOption> optionList=MajorCategoryConverter.INSTANCE.entities2Options(majorCategoryEntityList);

        log.info("optionList = {}",optionList);
        return optionList;
    }

    @Override
    public List<MajorCategoryElSelectOption> getMajorCategoryOptionListOfSecondLevel(Integer firstLevelId) {
        LambdaQueryWrapper<MajorCategoryEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MajorCategoryEntity::getLevel,2)
        .orderByAsc(MajorCategoryEntity::getLevelSort);
        if(firstLevelId == -1){
            wrapper.orderByAsc(MajorCategoryEntity::getLevel);
        }else{
            wrapper.eq(MajorCategoryEntity::getPid,firstLevelId);
        }

        List<MajorCategoryEntity> majorCategoryEntityList=baseMapper.selectList(wrapper);
        log.info("majorCategoryEntityList = {}",majorCategoryEntityList);
        List<MajorCategoryElSelectOption> optionList=MajorCategoryConverter.INSTANCE.entities2Options(majorCategoryEntityList);

        log.info("optionList = {}",optionList);
        return optionList;
    }
}
